package org.geotools.tutorial.quickstart.UI;

import org.geotools.tutorial.quickstart.Utilities.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @DesktopJavaDocable disable
 */
public class DialogHbaseLoader extends JDialog {

	private JLabel labelCatalog;
	private JTextField textFieldCatalog;

	private JLabel labelZookkeepers;
	private JTextField textFieldZookkeepers;

	private JLabel labelInstanceID;
	private JTextField textFieldInstanceID;
	// 用户名称
	private JLabel labelClient;
	private JTextField textFieldClient;
	// 密码
	private JLabel labelPassword;
	private JPasswordField textFieldPassword;

	private JButton buttonCancel;
	private JButton buttonOK;
	protected transient DialogResult dialogResult = DialogResult.NO;

	public Map getParams() {
		Map params = new HashMap();
		String user = this.textFieldClient.getText();
		if (user.length() == 0) {
			params.put("hbase.catalog", textFieldCatalog.getText());
			params.put("hbase.zookeepers", textFieldZookkeepers.getText());
			params.put("geomesa.stats.enable", true);
			params.put("geomesa.query.enable", true);
		} else {
			params.put("accumulo.instance.id", textFieldInstanceID.getText());
			params.put("accumulo.zookeepers", textFieldZookkeepers.getText());
			params.put("accumulo.user", textFieldClient.getText());
			params.put("accumulo.password", textFieldPassword.getText());
			params.put("accumulo.catalog", textFieldCatalog.getText());
		}
		return params;
	}

	public DialogResult getDialogResult() {
		return this.dialogResult;
	}

	public DialogHbaseLoader(JFrame owner) {
		super(owner, true);
		initComponent();
		initLayout();
	}

	public DialogResult showDialog() {
		try {
			setSize(350, 270);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.getDialogResult();
	}

	private void initComponent() {
		labelCatalog = new JLabel("Catalog(*):");
		labelZookkeepers = new JLabel("Zookeepers(*):");
		labelInstanceID = new JLabel("Instance.ID:");
		labelClient = new JLabel("User:");
		labelPassword = new JLabel("Password:");

		textFieldCatalog = new JTextField();
		textFieldZookkeepers = new JTextField();
		textFieldInstanceID = new JTextField();
		textFieldClient = new JTextField();
		textFieldPassword = new JPasswordField();

		buttonOK = new JButton("Open");
		buttonCancel = new JButton("Cancel");

		textFieldCatalog.setText("China");
		textFieldZookkeepers.setText("172.16.18.8:2181,172.16.18.9:2181,172.16.18.9:2182");
		textFieldInstanceID.setText("");
		textFieldClient.setText("");
		textFieldPassword.setText("");
		setTitle("Open HbaseDataStore");
		buttonOK.addActionListener((e) -> {
			this.dialogResult = DialogResult.OK;
			dispose();
		});
		buttonCancel.addActionListener((e) -> {
			this.dialogResult = DialogResult.CANCEL;
			dispose();
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		JPanel panelButton = new JPanel();
		panelButton.setLayout(new GridBagLayout());
		panel.add(this.labelCatalog, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldCatalog, new GridBagConstraintsHelper(1, 0, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 10).setWeight(1, 0));
		panel.add(this.labelZookkeepers, new GridBagConstraintsHelper(0, 1, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldZookkeepers, new GridBagConstraintsHelper(1, 1, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 10).setWeight(1, 0));
		panel.add(this.labelInstanceID, new GridBagConstraintsHelper(0, 2, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldInstanceID, new GridBagConstraintsHelper(1, 2, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 10).setWeight(1, 0));
		panel.add(this.labelClient, new GridBagConstraintsHelper(0, 3, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldClient, new GridBagConstraintsHelper(1, 3, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 10).setWeight(1, 0));
		panel.add(this.labelPassword, new GridBagConstraintsHelper(0, 4, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.NONE).setInsets(5, 10, 5, 10));
		panel.add(this.textFieldPassword, new GridBagConstraintsHelper(1, 4, 2, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setInsets(5, 0, 5, 10).setWeight(1, 0));
		panelButton.add(this.buttonOK, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.NONE).setInsets(0, 15, 10, 10).setWeight(1, 1));
		panelButton.add(this.buttonCancel, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.NONE).setInsets(0, 0, 10, 10).setWeight(0, 1));
		this.add(panel, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0).setInsets(5, 10, 5, 0));
		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 1, 1).setAnchor(GridBagConstraints.WEST).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
		this.add(panelButton, new GridBagConstraintsHelper(0, 2, 1, 1).setAnchor(GridBagConstraints.EAST).setFill(GridBagConstraints.HORIZONTAL).setWeight(1, 0));
	}

	public enum DialogResult {
		OK,
		CANCEL,
		NO,
	}
}
